import Cookies from 'js-cookie'


const app = {
  state: {
    language: Cookies.get('language') || 'ug',
    direction: Cookies.get('direction') || 'rtl',
    qiniu: {
      domain: ''
    },
    pageLoading: false,
    device: '',
    iosJsUrl: '',
    viewBoxScrollTop: 0
  },
  mutations: {
    SET_LANGUAGE: (state, language) => {
      state.language = language
      Cookies.set('language', language)
    },
    SET_DIRECTION: (state, direction) => {
      state.direction = direction
      Cookies.set('direction', direction)
    },
    SET_QINIU_DOMAIN: (state, domain) => {
      state.qiniu.domain = domain
    },
    SET_PAGE_LOADING: (state, status) => {
      state.pageLoading = status
    },
    SET_DEVICE: (state, device) => {
      state.device = device
    },
    SET_IOS_JS_URL: (state, iosJsUrl) => {
      state.iosJsUrl = iosJsUrl
    },
    SET_VIEWBOX_SCROLLTOP: (state, value) => {
      state.viewBoxScrollTop = value
    }
  },
  actions: {
    async setLanguage({ commit, dispatch }, language) {
      document.querySelector('html').setAttribute('lang', language)

      if (language === 'zh') {
        await dispatch('setDirection', 'ltr')
      } else if (language === 'ug') {
        await dispatch('setDirection', 'rtl')
      } else {
        await dispatch('setDirection', 'ltr')
      }

      commit('SET_LANGUAGE', language)
    },
    setDirection({ commit }, direction) {
      document.querySelector('html').setAttribute('dir', direction)
      commit('SET_DIRECTION', direction)
    },
    setPageLoading({ commit }, status) {
      commit('SET_PAGE_LOADING', status)
    }
  }
}

export default app
